/**
 *博尔信基础开发平台
 *<br> 版权声明：厦门博尔信软件有限公司版权所有,违者必究
 *<br> Copyright:  Copyright (c) 2012
 *<br> Company:pearlsoft
 *<br> @author 蒋勇华
 *<br> 2012-5-15
 *<br> @version 1.0
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *——————————————————————————————————————
 */
package com.pearlsoft.webapp.rms.resaudit.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pearlsoft.commons.utils.UUIDUtil;
import com.pearlsoft.util.DateUtils;
import com.pearlsoft.webapp.core.WebException;
import com.pearlsoft.webapp.core.dao.RecrodRowMapper;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.SimpleRecord;
import com.pearlsoft.webapp.core.service.BaseSimpleJdbcService;
import com.pearlsoft.webapp.rms.StepStatus;
import com.pearlsoft.webapp.rms.resapply.service.ResApplyService;

/**
 * 描述： 资源审核服务
 */
@Service
public class ResAuditService extends BaseSimpleJdbcService{
	@Autowired
	private ResApplyService resApplyService;
	private ResApplyService getResApplyService(){
		return this.resApplyService;
	}
	
	
	public Record getById(String stepId){
		String sql = "SELECT stepId,opinion,result,checkReport,flowId,applyId  FROM rms_flowstep  WHERE stepId=:stepId";
		Map<String,String> param = new HashMap<String,String>();
		param.put("stepId", stepId);
		Record record = this.getBaseSimpleJdbcDao().queryOne(sql,param,
			new  RecrodRowMapper<Record>(){
				@Override
				public Record mapRow(ResultSet rs, int rowNum)
						throws SQLException {				
					SimpleRecord recrod = new SimpleRecord();
					recrod.setValue("stepId", rs.getString(1));
					recrod.setValue("opinion", rs.getString(2));
					recrod.setValue("result", rs.getString(3));
					recrod.setValue("checkReport", rs.getString(4));	
					recrod.setValue("flowId", rs.getString(5));
					recrod.setValue("applyId", rs.getString(6));
					return recrod;
				}
			});
		return record;
	}
	/***
	 * 保存处理
	 * @param record
	 * @throws WebException
	 */
	public void save(Record record) throws WebException{
		String stepId =(String)record.getValue("stepId");
		String result =(String)record.getValue("result");
		String opinion =(String)record.getValue("opinion");
		String sql=" update rms_flowstep set result=:result,opinion=:opinion where stepId=:stepId";
		Map<String,String> param = new HashMap<String,String>();
		param.put("result", result);
		param.put("opinion", opinion);
		param.put("stepId", stepId);
		this.getBaseSimpleJdbcDao().update(sql, param);
	}
	
	/***
	 * 资源配置审批提交
	 * @param record
	 * @throws WebException
	 */
	public void apply(Record record)throws WebException{
		//保存或者更新资源配置审批
		this.save(record);
		String stepId =(String)record.getValue("stepId");
		//更新步骤状态
		this.endStep(record);
		
		Record preStep = this.getById(stepId);
		String preResult =(String)preStep.getValue("result");
		String applyId = (String) preStep.getValue("applyId");
		if("0".equalsIgnoreCase(preResult)){//审批不通过，结束流程
			this.endFlow(stepId,StepStatus.ENDFAIL);
		}else{
			//生成资源定时任务
			this.saveResTask(applyId);
			this.endFlow(stepId, StepStatus.UNRUN);
		}
	}
	
	
	/***
	 * 
	 * @param record
	 * @throws WebException
	 */
	public void endStep(Record record)throws WebException{
		String stepId =(String)record.getValue("stepId");
		//更新步骤状态
		String sql =" update rms_flowstep  set stepstatus=:stepstatus, endTime=:endTime where stepId=:stepId  ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("stepId", stepId);
		param.put("endTime", DateUtils.getCurrentDate());
		param.put("stepstatus", StepStatus.DEALED);
		this.getBaseSimpleJdbcDao().update(sql, param);
	}
	
	
	/***
	 * 生成资源定时任务
	 * @param applyId
	 * @throws WebException
	 */
	private void saveResTask(String applyId) throws WebException{
		String taskId = UUIDUtil.get();
		Record resRecord = this.getResApplyService().getById(applyId);
		String sql="insert into rms_restask(taskId,resName,applyer,applyId,startDate,endDate,status,crtTime)";
		sql= sql+"values( :taskId,:resName,:applyer,:applyId,:startDate,:endDate,:status,:crtTime)";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("taskId", taskId);
		param.put("resName", resRecord.getValue("resName"));
		param.put("applyer", resRecord.getValue("user_name")   );
		param.put("startDate", resRecord.getValue("startDate"));
		param.put("endDate", resRecord.getValue("endDate"));
		param.put("applyId", applyId);
		param.put("status", "0");
		param.put("crtTime", DateUtils.getCurrentDate());
		this.getBaseSimpleJdbcDao().update(sql, param);
	}
	
	/***
	 * 结束流程
	 * @param record
	 * @throws WebException
	 */
	private void endFlow(String preStepId,String stepstatus) throws WebException{
		Record preStep = this.getById(preStepId);
		String applyId = (String) preStep.getValue("applyId");
		Map<String, Object> param = new HashMap<String, Object>();
		//把申请步骤设置为完成
		String sql="update rms_flowstep t set t.stepstatus=:stepstatus where t.applyId=:applyId and t.stepType='0'";
		param.put("applyId", applyId);
		param.put("stepstatus", stepstatus);
		this.getBaseSimpleJdbcDao().update(sql, param);
	}
	
}
